<?php



class User extends database_object{
    
    public $id;
    public $firstname;
    public $lastname;
    public $username;
    public $yearborn;
    public $sex;
    public $zipcode;
    public $picture;

    protected static $table_name   = "bo_user";
    protected static $db_fields    = array("id", "firstname", "lastname", "yearborn", "sex", "zipcode", "picture", "username");
    
    
    
  
    
    // Method builds an object
    public function build($userobject){
        $userobject->save();
    }
      // Method that returns the users full name
    public function full_name(){
        
        return $firstname. " " . $lastname;
    }
    
    
    // Method checks if username is registered in database 
    public function usernameExists($username){
        
        $database   = Database::getInstance();
        $uname      = $database->escape_value($username);
        $sql        = "SELECT * FROM bo_user ";
        $sql       .= "WHERE username = '".$uname."';";
            
        if(self::find_by_sql($sql)!= null){

            return true;

        }
        else return false;

    }
        
    //Method finds the age on the current user object
    public function findAge(){
        $sql        = "SELECT * FROM bo_user ";
        $sql       .= "WHERE id = ".$this->id." ;";
        $yearborn1  = self::find_by_sql($sql)->yearborn;
        $nowyear    = date("Y", time());
        $yearborned = (int)$nowyear - (int)$yearborn1; 

        return  $yearborned;
    }
    
    
    // Returns an users age by its id
    public function findAgeById($id){
            $sql        = "SELECT * FROM bo_user ";
            $sql       .= "WHERE id = ".$id." ;";
            $yearborn1  = self::find_by_sql($sql)->yearborn;
            $nowyear    = date("Y", time());
            $yearborned = (int)$nowyear - (int)$yearborn1; 
           
           return  $yearborned;
    }
    
    
    // Method counds all the registered users of the system
    public function countAllUsers(){
       $totalusers = self::count_all();
       return $totalusers;
        
    }
    
    //method returns the 
    public function findAvgAge(){
        $y = self::find_all();
        
        foreach($y as $user){
           
            $tom += $user->findAgeByid($user->id);
        }
        
        return $tom / (count($y)+1);
 
    }
    
    
    // Method finds sex of the users visited the current con_id
    public function findSexByConid($con_id=0, $sex=""){
        
        $s = $sex;
        
        $sql = "SELECT count( DISTINCT bo_user.id ) as {$sex}\n"
             . "FROM bo_user\n"
             . "RIGHT JOIN bo_motion ON ( bo_user.id = bo_motion.user_id )\n"
             . "WHERE bo_user.sex = '{$s}'"
             . "AND bo_motion.company_product_id = {$con_id}";
             
         
         $res =  $this->executeSQL($sql);
         
         $num = $res[0][0];
         return $num;
    }
    
    // Gets an array of users and sorts them by sex
    public static function sortbySex($tab){
        
        $users  = $tab;
        $size   = count($users);
        $male   = 0;
        $female = 0;
        
        for($i=0; $i<$size; $i++){
            
            if($users[$i]->sex == "female"){
                $female++;
            }
            else if($users[$i]->sex == "male"){
                
                $male++;
            }
        }
        
        $table[] = $male;
        $table[] = $female;
        
        return $table;
    }
    
    //Method finds users aged between the variables $min and $max
    public function findByAge($min, $max){          
        $nowyear     = date("Y", time());
        $sql         =  "SELECT * FROM bo_user ";
        $sql        .=  "WHERE ".$nowyear." - yearborn > {$min} ";
        $sql        .=  "AND ".$nowyear." - yearborn >= {$max} ";

        return self::find_by_sql($sql);
    }
   
    
}

$user = new User();

?>
